package com.example.onlineeducation.ui.common

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.KeyEvent
import android.webkit.WebChromeClient
import com.example.onlineeducation.base.BaseWebActivity
import com.example.onlineeducation.databinding.WebPreviewActivityBinding
import com.yc.jetpacklib.extension.showToast
import com.yc.jetpacklib.extension.ycIsNotEmpty
import java.io.*


/**
 * SimpleDes:
 * Creator: Sindi
 * Date: 2022-01-12
 * UseDes:WebView预览
 */

class WebPreviewActivity : BaseWebActivity<WebPreviewActivityBinding>(WebPreviewActivityBinding::inflate) {

    companion object {
        const val KEY_URL = "key_url"
        fun newInstance(context: Context, url: String?) {
            if (url.ycIsNotEmpty()) {
                Intent(context, WebPreviewActivity::class.java).apply {
                    putExtra(KEY_URL, url)
                    context.startActivity(this)
                }
            } else context.showToast("url为空,请联系管理员")
        }
    }


    @SuppressLint("JavascriptInterface")
    override fun WebPreviewActivityBinding.initView() {
        mUrl = intent.getStringExtra(KEY_URL)!!
        webLayout.initWebviewSetting {
            actionbarLayout.setTitle(mTitle ?: "标题")
        }
        webLayout.webView.loadUrl(mUrl)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (event?.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
            finish()
        }
        return super.onKeyDown(keyCode, event)
    }

}
